package com.example.demo.controller;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
@RestController
@RequestMapping("/api/files")
public class FileController {

    private final Path fileStorageLocation = Paths.get("uploads").toAbsolutePath().normalize();



    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 生成唯一文件名
            String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
            Path targetLocation = fileStorageLocation.resolve(fileName);
            
            // 保存文件
            Files.copy(file.getInputStream(), targetLocation);
            
            return ResponseEntity.ok("文件上传成功: " + fileName);
        } catch (IOException ex) {
            return ResponseEntity.badRequest().body("文件上传失败: " + ex.getMessage());
        }
    }

    @GetMapping("/download/{fileName:.+}")
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileName) {
        try {
            Path filePath = fileStorageLocation.resolve("/Users/xiaren/Downloads/桌面驻留程序原型.zip").normalize();
            Resource resource = new UrlResource(filePath.toUri());

            if (resource.exists()) {
                return ResponseEntity.ok()
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException ex) {
            return ResponseEntity.badRequest().build();
        }
    }


    @GetMapping("/csv/stats")
    public String getCsvStats(@RequestParam(name = "num", defaultValue="10") Integer num) {
        // 使用ConcurrentHashMap来存储统计结果
        ConcurrentHashMap<String, Long> pathStats = new ConcurrentHashMap<>(1000_0000);
        AtomicLong totalLines = new AtomicLong(0);
        
        try {
            File directory = new File("/data/seaweedfs");
            if (directory.exists() && directory.isDirectory()) {
                File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
                
                if (files != null) {
                    log.info("开始处理，共发现 {} 个CSV文件", files.length);
                    
                    // 创建线程池，核心线程数设为CPU核心数
                    int processors = Runtime.getRuntime().availableProcessors();
                    ExecutorService executorService = Executors.newFixedThreadPool(processors);
                    CountDownLatch latch = new CountDownLatch(files.length);
                    
                    // 提交所有文件处理任务
                    for (int i = 0; i < files.length; i++) {
                        final File file = files[i];
                        final int fileIndex = i;
                        executorService.submit(() -> {
                            try {
                                long fileLines = 0;
                                long startTime = System.currentTimeMillis();
                                
                                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                                    String line;
                                    while ((line = reader.readLine()) != null) {
                                        totalLines.incrementAndGet();
                                        fileLines++;
                                        String[] columns = line.split(",");
                                        if (columns.length >= 3) {
                                            String name = columns[1].replaceAll("\"", "");
                                            if (name.contains(".")) {
                                                String path = columns[2].replaceAll("\"", "");
                                                String pathPrefix = path.length() > num ? path.substring(0, num) : path;
                                                pathStats.merge(pathPrefix, 1L, Long::sum);
                                            }
                                        }
                                    }
                                }
                                

                                long endTime = System.currentTimeMillis();
                                log.info("文件 {}/{}: {} - 处理完成，行数: {}，耗时: {}ms，当前不同路径数: {}", 
                                    fileIndex + 1, files.length, file.getName(), fileLines, endTime - startTime, pathStats.size());
                                
                            } catch (Exception e) {
                                log.error("处理文件失败: {}", file.getName(), e);
                            } finally {
                                latch.countDown();
                            }
                        });
                    }
                    
                    // 等待所有文件处理完成
                    latch.await();
                    executorService.shutdown();
                    
                    // 打印最终结果
                    log.info("所有文件处理完成，开始统计结果...");
                    pathStats.entrySet().stream()
                            .filter(entry -> entry.getValue() >= 10)
                            .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                            .forEach(entry -> log.info("路径前缀: {}, 出现次数: {}", entry.getKey(), entry.getValue()));
                    
                    log.info("统计--------------------------------------------------------------");
                    log.info("总行数: {}", totalLines.get());
                    log.info("不同路径前缀数: {}", pathStats.size());
                }
            }
        } catch (Exception e) {
            log.error("处理文件失败", e);
        }
        
        return "success";
    }

    @GetMapping("/csv/third-column")
    public String getThirdColumn(@RequestParam(name = "batchSize", defaultValue="1000000") Integer batchSize) {
        AtomicLong totalLines = new AtomicLong(0);
        
        try {
            File directory = new File("/data/seaweedfs");
            if (directory.exists() && directory.isDirectory()) {
                File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
                
                if (files != null) {
                    log.info("开始处理，共发现 {} 个CSV文件", files.length);
                    
                    // 创建线程池，核心线程数设为CPU核心数
                    int processors = Runtime.getRuntime().availableProcessors();
                    ExecutorService executorService = Executors.newFixedThreadPool(processors);
                    CountDownLatch latch = new CountDownLatch(files.length);
                    
                    // 提交所有文件处理任务
                    for (int i = 0; i < files.length; i++) {
                        final File file = files[i];
                        final int fileIndex = i;
                        executorService.submit(() -> {
                            try {
                                long fileLines = 0;
                                long startTime = System.currentTimeMillis();
                                
                                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                                    String line;
                                    while ((line = reader.readLine()) != null) {
                                        totalLines.incrementAndGet();
                                        fileLines++;
                                        String[] columns = line.split(",");
                                        if (columns.length >= 3) {
                                            String thirdColumn = columns[2].replaceAll("\"", "");
                                            log.info("文件: {}, 行号: {}, 第三列值: {}", file.getName(), fileLines, thirdColumn);
                                        }
                                        
                                        // 每处理batchSize行，打印一次进度
                                        if (fileLines % batchSize == 0) {
                                            long endTime = System.currentTimeMillis();
                                            log.info("文件 {}/{}: {} - 已处理行数: {}，耗时: {}ms", 
                                                fileIndex + 1, files.length, file.getName(), fileLines, endTime - startTime);
                                        }
                                    }
                                }
                                
                                long endTime = System.currentTimeMillis();
                                log.info("文件 {}/{}: {} - 处理完成，总行数: {}，总耗时: {}ms", 
                                    fileIndex + 1, files.length, file.getName(), fileLines, endTime - startTime);
                                
                            } catch (Exception e) {
                                log.error("处理文件失败: {}", file.getName(), e);
                            } finally {
                                latch.countDown();
                            }
                        });
                    }
                    
                    // 等待所有文件处理完成
                    latch.await();
                    executorService.shutdown();
                    
                    log.info("统计--------------------------------------------------------------");
                    log.info("所有文件处理完成，总行数: {}", totalLines.get());
                }
            }
        } catch (Exception e) {
            log.error("处理文件失败", e);
        }
        
        return "success";
    }

    @GetMapping("/csv/collect")
    public String collectData() {
        AtomicLong totalLines = new AtomicLong(0);
        AtomicLong validLines = new AtomicLong(0);
        
        // 创建月份列表
        List<String> months = new ArrayList<>();
        for (int year = 2024; year <= 2025; year++) {
            int startMonth = (year == 2024) ? 7 : 1;
            int endMonth = (year == 2025) ? 4 : 12;
            for (int month = startMonth; month <= endMonth; month++) {
                months.add(String.format("%04d-%02d", year, month));
            }
        }
        
        // 为每个月创建对应的BufferedWriter
        Map<String, BufferedWriter> writers = new ConcurrentHashMap<>();
        try {
            // 创建输出目录
            new File("/data/delData").mkdirs();
            
            // 初始化所有月份的writer
            for (String month : months) {
                String outputFile = String.format("/data/delData/collected_%s.txt", month.replace("-", ""));
                writers.put(month, new BufferedWriter(new FileWriter(outputFile)));
            }
            
            File directory = new File("/data/seaweedfs");
            if (directory.exists() && directory.isDirectory()) {
                File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
                
                if (files != null) {
                    log.info("开始处理，共发现 {} 个CSV文件", files.length);
                    
                    // 创建线程池，核心线程数设为CPU核心数
                    int processors = Runtime.getRuntime().availableProcessors();
                    ExecutorService executorService = Executors.newFixedThreadPool(processors);
                    CountDownLatch latch = new CountDownLatch(files.length);
                    
                    // 提交所有文件处理任务
                    for (int i = 0; i < files.length; i++) {
                        final File file = files[i];
                        final int fileIndex = i;
                        executorService.submit(() -> {
                            try {
                                long fileLines = 0;
                                long fileValidLines = 0;
                                long startTime = System.currentTimeMillis();
                                
                                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                                    String line;
                                    while ((line = reader.readLine()) != null) {
                                        totalLines.incrementAndGet();
                                        fileLines++;
                                        String[] columns = line.split(",");
                                        if (columns.length >= 3) {
                                            String secondColumn = columns[1].replaceAll("\"", "");
                                            String thirdColumn = columns[2].replaceAll("\"", "");
                                            
                                            // 检查条件：第二列包含. 且 第三列以/productInfo/开头
                                            if (secondColumn.contains(".") && thirdColumn.startsWith("/productInfo/")) {
                                                // 提取月份信息
                                                for (String month : months) {
                                                    if (thirdColumn.contains("/productInfo/" + month + "/")) {
                                                        validLines.incrementAndGet();
                                                        fileValidLines++;
                                                        
                                                        // 合并第三列和第二列
                                                        String mergedData = thirdColumn + secondColumn;
                                                        
                                                        // 写入对应月份的文件
                                                        synchronized (writers.get(month)) {
                                                            writers.get(month).write(mergedData);
                                                            writers.get(month).newLine();
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                
                                long endTime = System.currentTimeMillis();
                                log.info("文件 {}/{}: {} - 处理完成，总行数: {}，有效行数: {}，耗时: {}ms", 
                                    fileIndex + 1, files.length, file.getName(), fileLines, fileValidLines, endTime - startTime);
                                
                            } catch (Exception e) {
                                log.error("处理文件失败: {}", file.getName(), e);
                            } finally {
                                latch.countDown();
                            }
                        });
                    }
                    
                    // 等待所有文件处理完成
                    latch.await();
                    executorService.shutdown();
                    
                    // 关闭所有writer
                    for (BufferedWriter writer : writers.values()) {
                        writer.close();
                    }
                    
                    log.info("统计--------------------------------------------------------------");
                    log.info("所有文件处理完成，总行数: {}", totalLines.get());
                    log.info("符合条件的数据行数: {}", validLines.get());
                    log.info("数据已按月份分类保存到对应文件");
                }
            }
        } catch (Exception e) {
            log.error("处理文件失败", e);
        } finally {
            // 确保所有writer都被关闭
            for (BufferedWriter writer : writers.values()) {
                try {
                    writer.close();
                } catch (IOException e) {
                    log.error("关闭文件写入器失败", e);
                }
            }
        }
        
        return "success";
    }

    @GetMapping("/csv/batch-delete")
    public String batchDelete() {
        AtomicLong totalProcessed = new AtomicLong(0);
        AtomicLong successCount = new AtomicLong(0);
        AtomicLong failCount = new AtomicLong(0);
        
        try {
            File directory = new File("/tmp/delData");
            if (!directory.exists() || !directory.isDirectory()) {
                return "目录不存在: /tmp/delData";
            }
            
            File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".txt"));
            if (files == null || files.length == 0) {
                return "没有找到txt文件";
            }
            
            log.info("开始处理，共发现 {} 个txt文件", files.length);
            
            // 创建线程池
            int processors = Runtime.getRuntime().availableProcessors();
            ExecutorService executorService = Executors.newFixedThreadPool(processors);
            CountDownLatch latch = new CountDownLatch(files.length);
            
            // 为每个文件创建一个处理线程
            for (File file : files) {
                executorService.submit(() -> {
                    try {
                        log.info("开始处理文件: {}", file.getName());
                        long fileStartTime = System.currentTimeMillis();
                        long fileProcessed = 0;
                        long fileSuccess = 0;
                        long fileFail = 0;
                        
                        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                            String line;
                            while ((line = reader.readLine()) != null) {
                                fileProcessed++;
                                totalProcessed.incrementAndGet();
                                
                                try {
                                    // 构建删除请求URL

                                    StringBuffer sb = new StringBuffer(line);
                                    if (sb.length() < 37) {
                                        log.error("字符串长度不够:{}", line);
                                        continue;
                                    }
                                    sb.insert(36, '/');
                                    String deleteUrl = "http://10.100.1.200:8888" + sb;
                                    log.info("deleteUrl: {}", deleteUrl);
                                    
                                    // 发送删除请求
                                    ResponseEntity<String> response = new RestTemplate().exchange(
                                        deleteUrl,
                                        HttpMethod.DELETE,
                                        null,
                                        String.class
                                    );
                                    
                                    if (response.getStatusCode() == HttpStatus.OK || 
                                        response.getStatusCode() == HttpStatus.NO_CONTENT) {
                                        successCount.incrementAndGet();
                                        fileSuccess++;
                                    } else {
                                        failCount.incrementAndGet();
                                        fileFail++;
                                        log.error("删除失败 - URL: {}, 状态码: {}", deleteUrl, response.getStatusCode());
                                    }
                                    
                                    // 每处理1000条记录打印一次进度
                                    if (fileProcessed % 1000 == 0) {
                                        log.info("文件 {} - 已处理: {}, 成功: {}, 失败: {}", 
                                            file.getName(), fileProcessed, fileSuccess, fileFail);
                                    }
                                    
                                } catch (Exception e) {
                                    failCount.incrementAndGet();
                                    fileFail++;
                                    log.error("处理记录失败: {}", line, e);
                                }
                            }
                        }
                        
                        long fileEndTime = System.currentTimeMillis();
                        log.info("文件处理完成: {} - 总处理: {}, 成功: {}, 失败: {}, 耗时: {}ms", 
                            file.getName(), fileProcessed, fileSuccess, fileFail, fileEndTime - fileStartTime);
                            
                    } catch (Exception e) {
                        log.error("处理文件失败: {}", file.getName(), e);
                    } finally {
                        latch.countDown();
                    }
                });
            }
            
            // 等待所有文件处理完成
            latch.await();
            executorService.shutdown();
            
            log.info("批量删除任务完成 - 总处理: {}, 成功: {}, 失败: {}", 
                totalProcessed.get(), successCount.get(), failCount.get());
                
        } catch (Exception e) {
            log.error("批量删除任务执行失败", e);
            return "error: " + e.getMessage();
        }
        
        return String.format("success - 总处理: %d, 成功: %d, 失败: %d", 
            totalProcessed.get(), successCount.get(), failCount.get());
    }
    
    @GetMapping("/batchDeleteByFiles")
    public String batchDeleteByFiles() {
        String rootDir = "/Users/xiaren/Downloads";
        String baseUrl = "http://10.100.1.200:8888";
        AtomicLong totalProcessed = new AtomicLong(0);
        AtomicLong totalDeleted = new AtomicLong(0);
        
        // 定义要处理的文件列表
        String[] files = {
//            rootDir + "/specific_data/image_screen_data.txt",
            rootDir + "/specific_data/product_info_data.txt",
            rootDir + "/specific_data/tracking_no_data.txt"
        };
        
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        
        try {
            // 顺序处理每个文件
            for (String filePath : files) {
                File file = new File(filePath);
                if (!file.exists()) {
                    log.error("文件不存在: {}", filePath);
                    continue;
                }
                
                log.info("开始处理文件: {}", filePath);
                List<String> lines = new ArrayList<>();
                
                // 读取文件内容
                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        // 拼接完整URL
                        String fullUrl = baseUrl + line;
                        lines.add(fullUrl);
                    }
                }
                
                // 计算每个线程处理的行数
                int totalLines = lines.size();
                int batchSize = (totalLines + 7) / 8; // 向上取整，确保所有行都被处理
                
                // 创建新的CountDownLatch
                final CountDownLatch latch = new CountDownLatch(8);
                
                // 启动8个线程处理数据
                for (int i = 0; i < 8; i++) {
                    final int startIndex = i * batchSize;
                    final int endIndex = Math.min(startIndex + batchSize, totalLines);
                    
                    executorService.submit(() -> {
                        try {
                            for (int j = startIndex; j < endIndex; j++) {
                                String url = lines.get(j);
                                try {
                                    ResponseEntity<String> response = new RestTemplate().exchange(
                                        url,
                                        HttpMethod.DELETE,
                                        null,
                                        String.class
                                    );
                                    
                                    if (response.getStatusCode().is2xxSuccessful()) {
                                        totalDeleted.incrementAndGet();
                                    }
                                } catch (Exception e) {
                                    log.error("删除失败: {}", url, e);
                                }
                                
                                totalProcessed.incrementAndGet();
                                
                                // 每处理1000条打印一次进度
                                if (totalProcessed.get() % 1000 == 0) {
                                    log.info("已处理: {}, 成功删除: {}, 当前url:{}", totalProcessed.get(), totalDeleted.get(), url);
                                }
                            }
                        } finally {
                            latch.countDown();
                        }
                    });
                }
                
                // 等待当前文件的所有线程处理完成
                latch.await();
                log.info("文件处理完成: {}, 总处理数: {}, 成功删除: {}", filePath, totalProcessed.get(), totalDeleted.get());
            }
            
            return String.format("处理完成，总处理数: %d, 成功删除: %d", totalProcessed.get(), totalDeleted.get());
            
        } catch (Exception e) {
            log.error("处理失败", e);
            return "处理失败: " + e.getMessage();
        } finally {
            executorService.shutdown();
        }
    }
} 